﻿using LeSoft.Application.Bussiness;
using LeSoft.Application.Code;
using LeSoft.Application.Entity;
using LeSoft.Util;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace LeSoft.Application.Web.Areas.PropertyManage.Controllers
{
    public class CS_DeviceRepairController : MvcControllerBase
    {
       

        [HttpGet]
        [HandlerAuthorize(PermissionMode.Enforce)]
        public ActionResult Index()
        {
            return View();
        }
       
        [HttpGet]
        [HandlerAuthorize(PermissionMode.Enforce)]
        public ActionResult Form()
        {

            ViewBag.Mode = new CodeRuleBLL().CheckBillCodeMode(((int)CodeRuleEnum.DeviceRepairCode).ToString());
            return View();
        }

      



        private List<TreeStatusEntity> GetList()
        {
            DataTable query = new DeviceRepairBLL().GetCountList();
            List<TreeStatusEntity> li = new List<TreeStatusEntity>();

            if (query != null && query.Rows.Count > 0)
            {
                for (int i = 0; i < query.Columns.Count; i++)
                {
                    if (query.Columns[i].ColumnName == "all")
                        li.Add(new TreeStatusEntity("0", EnumAttribute.GetDescription(DRStateEnum.All), DRStateEnum.All.ToString(), query.Rows[0][i].ToInt()));
                    else if (query.Columns[i].ColumnName == "uncomplete")
                        li.Add(new TreeStatusEntity(DRStateEnum.All.ToString(), EnumAttribute.GetDescription(DRStateEnum.UnComplete), DRStateEnum.UnComplete.ToString(), query.Rows[0][i].ToInt()));
                    else if (query.Columns[i].ColumnName == "completed")
                        li.Add(new TreeStatusEntity(DRStateEnum.All.ToString(), EnumAttribute.GetDescription(DRStateEnum.Completed), DRStateEnum.Completed.ToString(), query.Rows[0][i].ToInt()));
                    else if (query.Columns[i].ColumnName == "wating")
                        li.Add(new TreeStatusEntity(DRStateEnum.UnComplete.ToString(), EnumAttribute.GetDescription(DRStateEnum.Wating), DRStateEnum.Wating.ToString(), query.Rows[0][i].ToInt()));
                    else if (query.Columns[i].ColumnName == "sending")
                        li.Add(new TreeStatusEntity(DRStateEnum.UnComplete.ToString(), EnumAttribute.GetDescription(DRStateEnum.Sending), DRStateEnum.Sending.ToString(), query.Rows[0][i].ToInt()));
                    else if (query.Columns[i].ColumnName == "uncheck")
                        li.Add(new TreeStatusEntity(DRStateEnum.Completed.ToString(), EnumAttribute.GetDescription(DRStateEnum.UnCheck), DRStateEnum.UnCheck.ToString(), query.Rows[0][i].ToInt()));
                    else if (query.Columns[i].ColumnName == "checked")
                        li.Add(new TreeStatusEntity(DRStateEnum.Completed.ToString(), EnumAttribute.GetDescription(DRStateEnum.Checked), DRStateEnum.Checked.ToString(), query.Rows[0][i].ToInt()));
                    else if (query.Columns[i].ColumnName == "unhand")
                        li.Add(new TreeStatusEntity(DRStateEnum.All.ToString(), EnumAttribute.GetDescription(DRStateEnum.UnHand), DRStateEnum.UnHand.ToString(), query.Rows[0][i].ToInt()));
                    else if (query.Columns[i].ColumnName == "handing")
                        li.Add(new TreeStatusEntity(DRStateEnum.UnComplete.ToString(), EnumAttribute.GetDescription(DRStateEnum.Handing), DRStateEnum.Handing.ToString(), query.Rows[0][i].ToInt()));
                    else
                        continue;

                }
            }
            return li;
        }

        /// <summary>
        /// 获取单据状态树
        /// </summary>
        /// <returns></returns>
        public ActionResult GetStateTreeList()
        {
            var data = GetList();
            List<TreeEntity> treeList = new List<TreeEntity>();
            foreach (TreeStatusEntity item in data)
            {
                TreeEntity tree = new TreeEntity();
                bool hasChildren = data.Count(t => t.PValue == item.Value) == 0 ? false : true;
                tree.id = item.Value;
                tree.text = item.Text + "(" + item.Count + ")";
                tree.value = item.Value;
                if (item.PValue == "0") tree.isexpand = true;
                else tree.isexpand = false;
                tree.complete = true;
                tree.hasChildren = hasChildren;
                tree.parentId = item.PValue;
                treeList.Add(tree);
            }
            return Content(treeList.TreeToJson());
        }


        /// <summary>
        /// 获取设备维修列表
        /// </summary>
        /// <param name="pagination"></param>
        /// <param name="queryJson"></param>
        /// <returns></returns>
        public ActionResult GetPageList(Pagination pagination, string queryJson)
        {
            var watch = CommonHelper.TimerStart();
            var data =new DeviceRepairBLL().GetPageList(pagination, queryJson);
            var JsonData = new
            {
                rows = data,
                total = pagination.total,
                page = pagination.page,
                records = pagination.records,
                costtime = CommonHelper.TimerEnd(watch)
            };
            return Content(JsonData.ToJson());
        }

        /// <summary>
        /// 获取设备维修表单数据
        /// </summary>
        /// <param name="keyValue"></param>
        /// <returns></returns>
        public ActionResult GetFormJson(string keyValue)
        {
            var data =new  DeviceRepairBLL().GetEntity(keyValue);
            var queryJson = new { BillId = keyValue }.ToJson();
            var file = new AttachFileBLL().GetList(keyValue).ToList();
            var query =new  DeviceRepairBLL().GetListDetail(queryJson);
            List<object> list = new List<object>();
            foreach (var item in query)
            {
                var device = new DeviceBLL().GetEntity(item.DeviceId);
                string DeviceName = "", PStructName = "", DeviceCode = "";
                if (device != null)
                {
                    DeviceName = device.Name;
                    DeviceCode = device.Code;
                    PStructName = new PStructBLL().GetEntity(device.PStructId).AllName;
                }
                var obj = new { item.Id, item.CheckDate, item.CheckMan, item.CheckManId, item.CheckMemo, item.CheckState, item.DeviceId, item.Issuse, item.State, item.WXBeginTime, item.WXContent, item.WXDate, item.WXEndTime, item.WXRId, item.WXRName, PStructName = PStructName, DeviceCode = DeviceCode, DeviceName = DeviceName };
                list.Add(obj);
            }
            var jsonData = new
            {
                data1 = data,
                data2 = file,
                data3 = list
            };
            return Content(jsonData.ToJson());

        }


        /// <summary>
        /// 提交表单
        /// </summary>
        /// <param name="keyValue"></param>
        /// <param name="entity"></param>
        /// <param name="listDetail"></param>
        /// <param name="listFile"></param>
        /// <returns></returns>

        [HttpPost]
        public ActionResult SaveForm(string keyValue, string entity, string listDetail, string listFile)
        {
            try
            {
                DeviceRepairEntity model = entity.ToObject<DeviceRepairEntity>();//维修单
                List<DeviceRepairDetailEntity> ListDetail = listDetail.ToObject<List<DeviceRepairDetailEntity>>();
                List<AttachFileEntity> list = listFile.ToObject<List<AttachFileEntity>>();
               new DeviceRepairBLL().SaveForm(keyValue, model, ListDetail, list);
                return Success("操作成功。");
            }
            catch (Exception e)
            {
                return Error(e.Message);
            }
        }

        [HttpPost]
        public ActionResult RemoveForm(string keyValue)
        {
            try
            {
                  new DeviceRepairBLL().RemoveForm(keyValue);
                return Success("删除成功！");
            }
            catch (Exception e)
            {
                return Error(e.Message);
            }
        }
    }
}
